package com.chunxuan.langquan.ui.activity.store;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.Base2Activity;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.CategoryEntity;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.support.util.GlideUtil;
import com.chunxuan.langquan.support.util.Logg;
import com.chunxuan.langquan.ui.activity.store.adapter.BaseAdapter;
import com.chunxuan.langquan.ui.activity.store.adapter.BaseViewHolder;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class ProductCategoryActivity extends Base2Activity {
    private RecyclerView rlvMain;
    private RecyclerView rlvSub;
    private Adapter mAdapterMain;
    private SubAdapter mSubAdapter;
    private ImageView ivBack;
    private int currentPosition = 0;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_product_category;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rlvMain = findViewById(R.id.rlv_product_main_category);
        rlvSub = findViewById(R.id.rlv_product_sub_category);
        ivBack = findViewById(R.id.iv_product_category_back);
        initView();

    }

    public void initView() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rlvMain.setLayoutManager(linearLayoutManager);
        mAdapterMain = new Adapter();
        rlvMain.setAdapter(mAdapterMain);
        final LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        rlvSub.setLayoutManager(linearLayoutManager1);
        mSubAdapter = new SubAdapter();
        rlvSub.setAdapter(mSubAdapter);
        requestCategoryData();
        rlvSub.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int position = linearLayoutManager1.findFirstVisibleItemPosition();
                if (position != -1) {
                    if (currentPosition != position) {
                        currentPosition = position;
                        rlvMain.smoothScrollToPosition(position);
                        mAdapterMain.setChoose(position);
                    }

                }
            }
        });

    }

    public void scrollRlvSub(int position) {
        rlvSub.smoothScrollToPosition(position);

    }

    public Disposable requestCategoryData() {
        return APIRetrofit.getDefault().requestCategoryData(Global.HEADER, null)
                .compose(RxUtil.<BaseResult2<List<CategoryEntity>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<CategoryEntity>>>() {
                    @Override
                    public void accept(BaseResult2<List<CategoryEntity>> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            List<CategoryEntity> categorys = result2.getData();
                            if (!isDataEmpty(categorys)) {
                                Logg.e("==全部类型==" + categorys.get(0));
                                categorys.get(0).setChoose(true);
                                mAdapterMain.setDatas(categorys);
                                mSubAdapter.setDatas(categorys);

                            }
                        } else {
                            ToastUtils.showShort(result2.getMsg());
                        }
//                        Logg.i("reqUserInfo.result2.getMsg()" + result2.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("获取全部类型失败");
                    }
                });
    }

    static class Adapter extends BaseAdapter<CategoryEntity> {

        @NonNull
        @Override
        public BaseViewHolder<CategoryEntity> onCreateViewHolder(@NonNull final ViewGroup parent,
                                                                 int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view = layoutInflater.inflate(R.layout.item_product_main_category, parent, false);
            final ViewHolder viewHolder = new ViewHolder(view);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ProductCategoryActivity) parent.getContext()).scrollRlvSub(viewHolder.getAdapterPosition());
                    setChoose(viewHolder.getAdapterPosition());
                }
            });
            return viewHolder;
        }

        public void setChoose(int position) {
            for (int i = 0; i < getDatas().size(); i++) {
                getDatas().get(i).setChoose(false);
            }
            getDatas().get(position).setChoose(true);
            notifyDataSetChanged();
        }
    }

    static class SubAdapter extends BaseAdapter<CategoryEntity> {

        @NonNull
        @Override
        public BaseViewHolder<CategoryEntity> onCreateViewHolder(@NonNull ViewGroup parent,
                                                                 int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view = layoutInflater.inflate(R.layout.item_product_sub_category_group, parent,
                    false);
            SubViewHolder viewHolder = new SubViewHolder(view);
            return viewHolder;
        }
    }

    static class SubAdapterItemAdapter extends BaseAdapter<CategoryEntity.ChildBean> {


        @NonNull
        @Override
        public BaseViewHolder<CategoryEntity.ChildBean> onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view = layoutInflater.inflate(R.layout.item_product_sub_category, parent, false);
            final SubAdapterItemHolder viewHolder = new SubAdapterItemHolder(view);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CategoryEntity.ChildBean bean = getDatas().get(viewHolder.getAdapterPosition());
                    ProductListActivity.actionStart(parent.getContext(), bean.getPid(),
                            bean.getId());
                }
            });
            return viewHolder;
        }
    }

    static class SubAdapterItemHolder extends BaseViewHolder<CategoryEntity.ChildBean> {
        ImageView iv;
        TextView tv;

        public SubAdapterItemHolder(@NonNull View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv_product_category_cover);
            tv = itemView.findViewById(R.id.tv_product_main_category);
        }

        @Override
        public void bindViewHolder(CategoryEntity.ChildBean value) {
            GlideUtil.loadImg(value.getImage(), iv);
            tv.setText(value.getName());
        }
    }


    static class ViewHolder extends BaseViewHolder<CategoryEntity> {
        TextView tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = this.itemView.findViewById(R.id.tv_product_main_category);
        }

        @Override
        public void bindViewHolder(CategoryEntity value) {
            if (value.isChoose()) {
                tv.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.text_main));
            } else {

                tv.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.text_black));
            }
            tv.setText(value.getName());
        }
    }

    static class SubViewHolder extends BaseViewHolder<CategoryEntity> {
        TextView tvTitle;
        RecyclerView rlv;
        SubAdapterItemAdapter adapterItemAdapter;

        public SubViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_product_main_category);
            rlv = itemView.findViewById(R.id.rlv_content);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(itemView.getContext(), 2);
            rlv.setLayoutManager(gridLayoutManager);
            adapterItemAdapter = new SubAdapterItemAdapter();
            rlv.setAdapter(adapterItemAdapter);
        }

        @Override
        public void bindViewHolder(CategoryEntity value) {
            tvTitle.setText(value.getName());
            adapterItemAdapter.setDatas(value.getChild());

        }
    }

}